<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <empy-title>
        <template v-slot:after>
          <el-button
            type="primary"
            size="small"
            @click="hAddPermission('0',1)"
          >添加权限</el-button>
        </template>
      </empy-title>
      <!-- 表格 -->
      <el-table
        border
        :data="list"
        row-key="id"
      >
        <el-table-column
          label="名称"
          prop="name"
        />
        <el-table-column
          align="center"
          label="标识"
          prop="code"
        />
        <el-table-column
          align="center"
          label="描述"
          prop="description"
        />
        <el-table-column
          align="center"
          label="操作"
        >
          <template slot-scope="{row}">
            <el-button
              v-if="row.type===1"
              type="text"
              @click="hAddPermission(row.id,row.type+1)"
            >添加</el-button>
            <el-button
              type="text"
              @click="hEditPermission(row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              @click="hDeletePer(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <dialog-tool
        ref="perDialog"
        :show-dialog.sync="showDialog"
        :pid="pid"
        :type-num="type"
        @update="hUpdate"
      />
    </div>
  </div>
</template>

<script>
import { getPermissionAll, deletePermissionById } from '@/api/permission'
import { transData } from '@/utils'
import DialogTool from './components/dialogTool'
export default {
  components: {
    DialogTool
  },
  data () {
    return {
      showDialog: false,
      pid: '',
      type: 0,
      list: []
    }
  },

  created () {
    this.getPermissionAll()
  },
  methods: {
    async getPermissionAll () {
      // const res = await getPermissionAll()
      // console.log(res)
      this.list = transData(await getPermissionAll(), '0')
    },
    hAddPermission (pid, type) {
      this.pid = pid
      this.type = type
      console.log(this.pid)
      console.log(this.type)
      this.showDialog = true
    },
    hEditPermission (id) {
      this.$refs.perDialog.getPermissionById(id)
      this.showDialog = true
      console.log(id)
    },
    hUpdate () {
      this.getPermissionAll()
    },
    async hDeletePer (id) {
      await this.$confirm('确定要删除么?')
      await deletePermissionById(id)
      this.$message.success('删除成功')
      this.getPermissionAll()
    }
  }
}
</script>

